/**
 * 
 */
package leetCode;

/**
 * @author zhong
 *
 */
public class KokoEatingBananas {
	public int minEatingSpeed(int[] piles, int H) {
		int max = 0;
		for (int i = 0; i < piles.length; i++) {
			max = Math.max(piles[i], max);
		}
		int low = 1, high = max;
		while (low < high) {
			int mid = (low + high) / 2;
			System.out.println(low + "___" + high + "__" + mid);
			if (finish(mid, piles, H)) {// 可以尝试慢一点的速度
				high = mid;
			} else {
				low = mid + 1;
			}
		}
		return low;
	}

	/**
	 * 能否在H小时内吃完piles数组代表的香蕉，速度为mid
	 * 
	 * @param speed
	 * @param piles
	 * @param h
	 * @return
	 */
	private boolean finish(int speed, int[] piles, int h) {
		int totalTime = 0;
		double sppedVal = speed;
		for (int p : piles) {
			totalTime += Math.ceil(p / sppedVal);
			if (totalTime > h) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] piles = { 3, 6, 7, 11 };
		int h = 8;
		System.out.println(new KokoEatingBananas().minEatingSpeed(piles, h));
	}

}
